<template>
  <div class="departments-container">
    <div class="app-container">
      <el-card v-loading="loading" class="tree-card">
        <!-- 用了一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <span>{{ company.name }}</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex">
              <!-- 两个内容 -->
              <el-col :span="12">负责人</el-col>
              <el-col :span="12">
                <!-- 下拉菜单 element -->
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <!-- 下拉菜单 -->
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="addList(company)">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <el-tree :data="departs" :props="defaultProps" :default-expand-all="true">
          <template #default="{data}">
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px ; width : 100%">
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex">
                  <!-- 两个内容 -->
                  <el-col :span="12">{{ data.manager }}</el-col>
                  <el-col :span="12">
                    <!-- 下拉菜单 element -->
                    <el-dropdown @command="handeler">
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item :command="{cmd:'add', id : data.id , data:data}">添加子部门</el-dropdown-item>
                        <el-dropdown-item :command="{cmd:'edit',data:data}">修改部门</el-dropdown-item>
                        <el-dropdown-item :command="{cmd:'del', id : data.id}">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
    </div>
    <!-- 如果父组件想让子组件修改父组件中传过去的值，就必须把这个值放权给子组件
     父传子的时候，使用sync修饰符，表示父组件向子组件放权了，
     子组件修改放权过来的值，要使用固定语法：this.$emit('update:props中的属性'，要修改的值)
     -->
    <addDept ref="addpetd" :show-dialog.sync="showDialog" :notedata="notedata" :data-list="dataList" />
  </div>
</template>

<script>
import addDept from './components/add-dept.vue'
import { transList } from '@/utils/index'
import { getUserListAPI, deleteListAPI } from '@/api/demprments'
export default {
  name: 'Departments',
  components: {
    addDept
  },
  data() {
    return {
      departs: [],
      defaultProps: {
        label: 'name'
      },
      company: {},
      showDialog: false,
      notedata: {},
      dataList: [],
      loading: false
    }
  },
  created() {
    this.UserList()
  },
  methods: {
    async UserList() {
      this.loading = true
      const res = await getUserListAPI()
      console.log(res)
      this.departs = transList(res.data.depts, '')
      this.dataList = res.data.depts
      this.company.name = res.data.companyName
      this.company.id = ''
      this.company.children = this.departs
      this.loading = false
    },
    handeler({ cmd, id, data }) {
      if (cmd === 'add') {
        console.log('点击了添加按钮')
        this.addList(data)
      }
      if (cmd === 'edit') {
        console.log('点击了修改按钮')
        this.updataList(data)
      }
      if (cmd === 'del') {
        console.log('点击了删除按钮')
        this.deleteList(id)
      }
    },
    /**
     * 添加
     */
    addList(data) {
      this.showDialog = true
      this.notedata = data
      // this.$refs.addpetd.MenagerList()
    },
    /**
     * 修改
     */
    updataList(data) {
      this.showDialog = true
      this.notedata = data
      // 当dom执行完
      this.$nextTick(() => {
        this.$refs.addpetd.getMenager()
      })
    },
    /**
     * 删除
     */
    deleteList(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await deleteListAPI(id)
        this.UserList()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    closeDept() {
      this.showDialog = false
    }
  }
}
</script>

<style scoped lang="scss">
.tree-card {
  padding: 30px 30px;
  font-size:14px;
}
.el-tree {
  ::v-deep {
    // 小三角的样式, 去掉默认的小三角的旋转效果
    .el-tree-node__expand-icon.expanded {
      -webkit-transform: rotate(0deg);
      transform: rotate(0deg);
    }
    // 有子节点 且未展开 +
    .el-icon-caret-right:before {
      background: url("~@/assets/common/add-circle.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    // 有子节点 且已展开 -
    .el-tree-node__expand-icon.expanded.el-icon-caret-right:before{
      background: url("~@/assets/common/minus-circle.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    // 没有子节点
    .el-tree-node__expand-icon.is-leaf::before  {
      background: url("~@/assets/common/user-filling.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
  }
}
</style>
